package application;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;

/**
 * Slouzi pro stahovani a parsovani jidelnicku ze strava.cz.
 * 
 * @author Tomas Zima
 */
public class DiningList
{
	/** URL adresa, ze ktere se bude nacitat web. */
	private final static	String	PAGE_URL		= "http://www.strava.cz/foxisapi/foxisapi.dll/istravne.istravne.process?form1jidelnicky&zarizeni=3199";
	
	/** Adresa proxy serveru. */
	private final static	String	PROXY_ADDRESS	= "10.1.10.6";
	
	/** Port proxy serveru. */
	private final static	int		PROXY_PORT		= 8080;
	
	/** Pouzit proxy server? */
	private final static	boolean	USE_PROXY		= false;
	
	/** DOM webove stranky s jidelnickem. */
	private					Document	document;

	/**
	 * Konstruktor.
	 */
	public DiningList()
	{
		loadPage();
	}
	
	/**
	 * Konstruktor.
	 * Stahne HTML kod stranky a pomoci JTidy z nej vygeneruje DOM objekt.
	 */
	public void loadPage()
	{
		try
		{
			/*
			 * Pomocne promenne.
			 */
			StringBuffer		text		= new StringBuffer();
			URL					page		= new URL(PAGE_URL);
			Proxy				proxy		= new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_ADDRESS, PROXY_PORT));
			HttpURLConnection	conn;
			String				html;
			
			/*
			 * Vytvorit pripojeni pres proxy?
			 */
			if (USE_PROXY)
			{
				conn = (HttpURLConnection) page.openConnection(proxy);
			}
			else
			{
				conn = (HttpURLConnection) page.openConnection();
			}
			
			// Vytvorit pripojeni
			conn.connect();
			
			/*
			 * Pomocne promenne.
			 */
			InputStreamReader in	= new InputStreamReader((InputStream) conn.getContent());
			BufferedReader buff		= new BufferedReader(in);
			String line;

			// Precte veskera prichozi data a ulozi je
			while (buff.ready())
			{
				line = buff.readLine();
				text.append(line + "\n");
			}

			// Konverze prichozich dat na HTML
			html = text.toString();

			// Inicializace JTidy
			Tidy tidy = new Tidy();
		
			// Konfigurace JTidy
			tidy.setInputEncoding("cp1250");
			tidy.setOutputEncoding("utf-8");
			tidy.setForceOutput(true);
			tidy.setShowErrors(0);
			tidy.setQuiet(true);
			
			// Ziskani DOM objektu
			this.document = tidy.parseDOM(new ByteArrayInputStream(html.getBytes()), null);
		}
		catch (MalformedURLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void parseAll()
	{
		//
	}
	
	/**
	 * Reprezentuje jeden zaznam, tzn.
	 * jidlo na dany den. 
	 */
	private final class Record
	{
		/** Datum ciselne (napr. "16.04."). */
		String		dayNumeric;
		
		/** Jmeno dne (pondeli, utery, ...). */
		String		dayName;
		
		/** Cely nazev jidla. */
		String		food;
	}
}
